/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * StartStop_Home.java
 *
 * Created on Oct 2, 2014, 4:03:29 PM
 */
package com.JD.StartStopMachine;

import com.JD.StartStop.Hibernate.Finalmachinestartstoprecord;
import com.JD.datePicker.DatePicker;
import com.JD.home.JD_Home;
import com.JD.master.Hibernate.PartyMaster;
import com.JD.master.Validation.Validator;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.JPanel;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author admin
 */
public class StartStop_Home extends javax.swing.JFrame {

    /** Creates new form StartStop_Home */
    com.JD.master.Validation.Validator validator = new Validator();
    String rawField1 = "NO";
    String rawField2 = "NO";
    String rawField3 = "NO";
    String rawField4 = "NO";
    String machineName = "";
    String startDate = "";
    String startTime = "";
    String stopDate = "";
    String stopTime = "";
    String totalTime = "";
    String startReading = "";
    String stopReading = "";
    String totalFuelUsed = "";
    String average = "";
    String remark = "";

    public StartStop_Home() {
        initComponents();


        start_Button.setVisible(false);
        stop_Button.setVisible(false);


        SessionFactory sessionFactory = com.JD.master.Hibernate.Master_HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();

        //  com.JD.master.Hibernate.MachineMaster

        Query query = session.createQuery("from com.JD.master.Hibernate.MachineMaster");

        List list = query.list();


        for (Object object : list) {

            com.JD.master.Hibernate.MachineMaster machineMaster = (com.JD.master.Hibernate.MachineMaster) object;

            machineList_Combobox.addItem(machineMaster.getMachineName());
        }



        dateFromStart_TextField.setText(new SimpleDateFormat("dd/MM/yyyy").format(new Date()));

        timeFromStart_TextField.setText(new SimpleDateFormat("HH:mm").format(new Date()));








    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        startStop_Panel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        partyTable = new javax.swing.JTable();
        machineList_Combobox = new javax.swing.JComboBox();
        go_Button = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        dateFromStart_TextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        timeFromStart_TextField = new javax.swing.JTextField();
        start_Button = new javax.swing.JButton();
        stop_Button = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        reading_TextField = new javax.swing.JTextField();
        refresh_Button = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        startStop_Panel.setBackground(new java.awt.Color(255, 102, 51));
        startStop_Panel.setPreferredSize(new java.awt.Dimension(1135, 627));

        partyTable.setFont(new java.awt.Font("Traditional Arabic", 0, 14));
        partyTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Machine Name", "Status"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(partyTable);

        machineList_Combobox.setFont(new java.awt.Font("Traditional Arabic", 0, 14));

        go_Button.setFont(new java.awt.Font("Traditional Arabic", 1, 14));
        go_Button.setText("Go");
        go_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                go_ButtonActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Traditional Arabic", 1, 14));
        jLabel1.setText("Date:");

        dateFromStart_TextField.setFont(new java.awt.Font("Traditional Arabic", 0, 14));

        jLabel2.setFont(new java.awt.Font("Traditional Arabic", 1, 14));
        jLabel2.setText("Time:");

        timeFromStart_TextField.setFont(new java.awt.Font("Traditional Arabic", 0, 14));

        start_Button.setFont(new java.awt.Font("Traditional Arabic", 1, 14));
        start_Button.setText("Start");
        start_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                start_ButtonActionPerformed(evt);
            }
        });

        stop_Button.setFont(new java.awt.Font("Traditional Arabic", 1, 14));
        stop_Button.setText("Stop");
        stop_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stop_ButtonActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Traditional Arabic", 1, 14));
        jLabel3.setText("Reading:");

        reading_TextField.setFont(new java.awt.Font("Traditional Arabic", 0, 14));

        refresh_Button.setFont(new java.awt.Font("Traditional Arabic", 1, 14));
        refresh_Button.setText("Refresh ");
        refresh_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refresh_ButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout startStop_PanelLayout = new javax.swing.GroupLayout(startStop_Panel);
        startStop_Panel.setLayout(startStop_PanelLayout);
        startStop_PanelLayout.setHorizontalGroup(
            startStop_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(startStop_PanelLayout.createSequentialGroup()
                .addGroup(startStop_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1050, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(startStop_PanelLayout.createSequentialGroup()
                        .addGap(302, 302, 302)
                        .addGroup(startStop_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(startStop_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(startStop_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(dateFromStart_TextField)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, startStop_PanelLayout.createSequentialGroup()
                                    .addComponent(start_Button, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                                    .addComponent(stop_Button, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(timeFromStart_TextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                                .addComponent(reading_TextField))
                            .addComponent(machineList_Combobox, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(36, 36, 36)
                        .addGroup(startStop_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(go_Button, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(refresh_Button))))
                .addContainerGap(125, Short.MAX_VALUE))
        );
        startStop_PanelLayout.setVerticalGroup(
            startStop_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, startStop_PanelLayout.createSequentialGroup()
                .addGap(67, 67, 67)
                .addGroup(startStop_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(machineList_Combobox, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(go_Button))
                .addGroup(startStop_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(startStop_PanelLayout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addGroup(startStop_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(dateFromStart_TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(startStop_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(timeFromStart_TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(startStop_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(reading_TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(32, 32, 32)
                        .addGroup(startStop_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(start_Button)
                            .addComponent(stop_Button)))
                    .addGroup(startStop_PanelLayout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addComponent(refresh_Button)))
                .addGap(202, 202, 202)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(startStop_Panel, javax.swing.GroupLayout.DEFAULT_SIZE, 1175, Short.MAX_VALUE)
                .addGap(20, 20, 20))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(startStop_Panel, javax.swing.GroupLayout.PREFERRED_SIZE, 718, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void go_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_go_ButtonActionPerformed
        // TODO add your handling code here:

        SessionFactory sessionFactory = com.JD.master.Hibernate.Master_HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();


        Criteria cr = session.createCriteria(com.JD.master.Hibernate.MachineMaster.class);
        cr.add(Restrictions.eq("machineName", machineList_Combobox.getSelectedItem().toString()));
        List results = cr.list();

        for (Object object : results) {
            com.JD.master.Hibernate.MachineMaster machineMaster = (com.JD.master.Hibernate.MachineMaster) object;

            if (machineMaster.getRawField1().equals("STOP")) {

                start_Button.setVisible(true);
                stop_Button.setVisible(false);

            } else {
                stop_Button.setVisible(true);
                start_Button.setVisible(false);
            }

        }



    }//GEN-LAST:event_go_ButtonActionPerformed

    private void start_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_start_ButtonActionPerformed
        // TODO add your handling code here:


        rawField1 = "START";
        rawField2 = dateFromStart_TextField.getText() + "##" + reading_TextField.getText();
        rawField3 = timeFromStart_TextField.getText();

        com.JD.Static.GetObject.lable1FromToolBox_Lable.setText(machineList_Combobox.getSelectedItem().toString());




        SessionFactory sessionFactory = com.JD.master.Hibernate.Master_HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();


        Criteria cr = session.createCriteria(com.JD.master.Hibernate.MachineMaster.class);
        cr.add(Restrictions.eq("machineName", machineList_Combobox.getSelectedItem().toString()));
        List results = cr.list();

        for (Object object : results) {
            com.JD.master.Hibernate.MachineMaster machineMaster = (com.JD.master.Hibernate.MachineMaster) object;

            machineMaster.setRawField1(rawField1);
            machineMaster.setRawField2(rawField2);
            machineMaster.setRawField3(rawField3);
            session.save(machineMaster);
            transaction.commit();


        }

        dateFromStart_TextField.setText("");
        timeFromStart_TextField.setText("");
        stop_Button.setVisible(true);
        start_Button.setVisible(false);
        dateFromStart_TextField.setText(new SimpleDateFormat("dd/MM/yyyy").format(new Date()));

        timeFromStart_TextField.setText(new SimpleDateFormat("HH:mm").format(new Date()));

        reading_TextField.setText("");



    }//GEN-LAST:event_start_ButtonActionPerformed

    private void stop_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stop_ButtonActionPerformed
        // TODO add your handling code here:

        SessionFactory sessionFactory = com.JD.master.Hibernate.Master_HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();


        Criteria cr = session.createCriteria(com.JD.master.Hibernate.MachineMaster.class);
        cr.add(Restrictions.eq("machineName", machineList_Combobox.getSelectedItem().toString()));
        List results = cr.list();

        for (Object object : results) {
            com.JD.master.Hibernate.MachineMaster machineMaster = (com.JD.master.Hibernate.MachineMaster) object;

            String data[] = machineMaster.getRawField2().split("##");

            startDate = data[0];
            startReading = data[1];

            startTime = machineMaster.getRawField3();

            totalFuelUsed = machineMaster.getRawField4();


            machineMaster.setRawField1("STOP");

            session.save(machineMaster);
            transaction.commit();




        }


        stopDate = dateFromStart_TextField.getText();
        stopTime = timeFromStart_TextField.getText();


        stopReading = reading_TextField.getText();

        machineName = machineList_Combobox.getSelectedItem().toString();

        totalTime = "";


        storeData();

        dateFromStart_TextField.setText("");
        timeFromStart_TextField.setText("");
        reading_TextField.setText("");

        com.JD.Static.GetObject.lable1FromToolBox_Lable.setText("");

        stop_Button.setVisible(false);
        start_Button.setVisible(true);

        dateFromStart_TextField.setText(new SimpleDateFormat("dd/MM/yyyy").format(new Date()));

        timeFromStart_TextField.setText(new SimpleDateFormat("HH:mm").format(new Date()));

        reading_TextField.setText("");



    }//GEN-LAST:event_stop_ButtonActionPerformed

    private void refresh_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refresh_ButtonActionPerformed
        // TODO add your handling code here:

        dateFromStart_TextField.setText(new SimpleDateFormat("dd/MM/yyyy").format(new Date()));

        timeFromStart_TextField.setText(new SimpleDateFormat("HH:mm").format(new Date()));


    }//GEN-LAST:event_refresh_ButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField dateFromStart_TextField;
    private javax.swing.JButton go_Button;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox machineList_Combobox;
    private javax.swing.JTable partyTable;
    private javax.swing.JTextField reading_TextField;
    private javax.swing.JButton refresh_Button;
    public javax.swing.JPanel startStop_Panel;
    private javax.swing.JButton start_Button;
    private javax.swing.JButton stop_Button;
    private javax.swing.JTextField timeFromStart_TextField;
    // End of variables declaration//GEN-END:variables

    public JPanel getStartStop_Panel() {
        return startStop_Panel;
    }

//String machineName,String startDate,String startTime,String stopDate,String stopTime,String totalTime,String startReading,String stopReading,String totalFuelUsed,String average,String remark
    void storeData() {
        SessionFactory sessionFactory = com.JD.StartStop.Hibernate.StartStop_HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();


        // com.JD.StartStop.Hibernate
        com.JD.StartStop.Hibernate.Finalmachinestartstoprecord finalmachinestartstoprecord = new Finalmachinestartstoprecord(machineName, startDate, startTime, stopDate, stopTime, totalTime, startReading, stopReading, totalFuelUsed, average, remark, rawField1, rawField2, rawField3, rawField4);
        session.save(finalmachinestartstoprecord);
        transaction.commit();



    }
}
